#include <stdafx.h>
#include "ClientChannelManager.h"
#include "ClientChannelCore.h"

namespace
{
    rdc_transport::ClientChannelManager g_ChannelManager;
}

void rdc_transport::ClientChannelManager::ChannelInitEventProc(LPVOID pInitHandle, UINT event, LPVOID pData, UINT dataLength) throw()
{
    boost::shared_lock<boost::shared_mutex> l(m_channelsAcessLock);

    BOOST_FOREACH( std::vector<pClientChannel>::value_type p, m_channels)
    {
        p->ChannelInitEvent(pInitHandle, event, pData, dataLength);
    }
}

void rdc_transport::ClientChannelManager::ChannelOpenEventProc(DWORD openHandle, UINT event, LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags) throw()
{
    boost::shared_lock<boost::shared_mutex> l(m_channelsAcessLock);

    BOOST_FOREACH( std::vector<pClientChannel>::value_type p, m_channels)
    {
        p->ChannelOpenEvent(openHandle, event, pData, dataLength, totalLength, dataFlags);
    }
}

void rdc_transport::ClientChannelManager::AddChannel(ICliChannalCore* channel)
{
    boost::unique_lock<boost::shared_mutex> l(m_channelsAcessLock);

    m_channels.insert(channel);
}

void rdc_transport::ClientChannelManager::DeleteChannel(ICliChannalCore* channel)
{
    boost::unique_lock<boost::shared_mutex> l(m_channelsAcessLock);

    m_channels.erase(channel);
}

void VCAPITYPE rdc_transport::ChannelInitEventProc(LPVOID pInitHandle, UINT event, LPVOID pData, UINT dataLength)
{
    UNREFERENCED_PARAMETER(pData);
    UNREFERENCED_PARAMETER(dataLength);

    g_ChannelManager.ChannelInitEventProc(pInitHandle, event, pData, dataLength);
}

void VCAPITYPE rdc_transport::ChannelOpenEventProc(DWORD openHandle, UINT event, LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
{
    UNREFERENCED_PARAMETER(pData);
    UNREFERENCED_PARAMETER(dataLength);

    g_ChannelManager.ChannelOpenEventProc(openHandle, event, pData, dataLength, totalLength, dataFlags);
}

rdc_transport::ClientChannelManager* rdc_transport::GetClientChannelManager()
{
    return &g_ChannelManager;
}